<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once APPPATH . 'controllers/application' . EXT;

class Gio_hang extends Application {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->titlePage = 'giỏ hàng';
        $data = array();
        $data["spxn"] = $this->product_model->getProduct(NULL,TRUE)->result();
        $this->render($this->load->view('hienthi_giohang', $data, TRUE));
    }

	public function addCart() {
        $fid = $this->input->post('fid');
        $fname = $this->input->post('fname');
        $fprice = $this->input->post('fprice');
        $fimg = $this->input->post('fimg');
        $data = array(
            'id' => $fid,
            'qty' => 1,
            'name' => $fname,
            'price' => $fprice,
            'img' => $fimg
        );

        $this->cart->insert($data);
        redirect('gio_hang');
    }
	
    public function ht_dat_hang() {
        $this->titlePage = "Mua hàng, đăng ký, đăng nhập";
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $id = $session_data['id'];

            $data['content_user'] = $this->user_model->getUserToID($id)->row();
            $data['pay'] = $this->pay_model->getPay();
            $this->render($this->load->view('dat_hang_view', $data, TRUE));
        } else {
            $this->render($this->load->view("dat_hang_step1_view","",TRUE));
        }
    }

    public function update_remove_order() {

        if ($this->input->post('update')) {
            for ($i = 1; $i <= $this->cart->total_items(); $i++) {
                $item = $this->input->post($i);
                $data = array(
                    'rowid' => $item['rowid'],
                    'qty' => $item['qty']
                );
                $this->cart->update($data);
            }
            redirect('gio_hang', 'refresh');
        }
        if ($this->input->post('remove')) {
            $this->cart->destroy();
            redirect('gio_hang', 'refresh');
        }
        if ($this->input->post('check_out')) {
            redirect("gio_hang/ht_dat_hang");
        }
    }

    public function delete($rowid) {
        $data = array(
            'rowid' => $rowid,
            'qty' => 0
        );
        $this->cart->update($data);
        redirect('gio_hang', 'refresh');
    }

    public function dat_hang() {
        //set_rule validation
        $config = array(
            array(
                'field' => 'fullname1',
                'label' => 'Họ tên người nhận',
                'rules' => 'required|min_length[4]|xss_clean'
            ),
            array(
                'field' => 'address1',
                'label' => 'Địa chỉ',
                'rules' => 'required|xss_clean'
            ),
            array(
                'field' => 'email',
                'label' => 'email',
                'rules' => 'required|valid_email|xss_clean'
            ),
            array(
                'field' => 'paymentmethod',
                'label' => 'Phương thức thanh toán',
                'rules' => 'required'
            ),
            array(
                'field' => 'todays_date',
                'label' => 'Ngày nhận hàng',
                'rules' => 'required'
            ),
            array(
                'field' => 'phoneContact',
                'label' => 'Số điện thoại liên lạc',
                'rules' => 'required|numeric'
            )
        );
        //mang nhap
        $name = $this->input->post('fullname1');
        $addr = $this->input->post('address1');
        $email = $this->input->post('email');
        $pay = $this->input->post('paymentmethod');
        $osdate = $this->input->post('todays_date');
        $a = strtotime($osdate);
        $b = date("Y-m-d", $a);
        $phone = $this->input->post('phoneContact');
        $ngay = date("d");
        $thang = date("m");
        $nam = date("Y");
        $curdate = $nam . "-" . $thang . "-" . $ngay;
        $session_data = $this->session->userdata('logged_in');
        $id = $session_data['id'];
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() === FALSE) {
            $this->ht_dat_hang();
        } else {
            $result = $this->order_model->Add_Order($name, $addr, $pay, $b, $curdate, $phone, $id);
            $oid = $this->order_model->GetOID($id);
            if ($result === TRUE) {
                foreach ($this->cart->contents() as $items) {
                    foreach ($oid as $row) {
                        $result2 = $this->order_model->Add_order_detail($row->OID, $items['id'], $items['qty'], $items['price'], $items['subtotal']);
                    }
                }
                if ($result2 === TRUE) {
                    $this->cart->destroy();
                    $data['pdBS'] = $this->product_model->getProductBS()->result();
                    $this->render($this->load->view('mua_thanh_cong_view', $data, TRUE));
                }
            } else {
                echo ("<script>alert('co loi');window.history.back();</script>");
            }
        }
    }

}

?>
